package preprocessing;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class JoinSumHourAndWeather {
    
    private static Map<String, String> weatherMap = new HashMap<String, String>();
    
    public static void main(String[] args) throws Exception {
        
        // The name of the file to open.
        //hour by type
//        String sumFile = "/home/ivy/my_hadoop/passenger/result/sum_hour_type.txt";
//        String weatherFile = "/home/ivy/my_hadoop/passenger/result/weather.txt";
//        String outputFile = "/home/ivy/my_hadoop/passenger/result/result_hour_type.txt";
        
        //hour
        String sumFile = "/home/ivy/my_hadoop/passenger/result/sum_hour.txt";
        String weatherFile = "/home/ivy/my_hadoop/passenger/result/weather.txt";
        String outputFile = "/home/ivy/my_hadoop/passenger/result/result_hour.txt";
        
        JoinSumHourAndWeather.generateWeatherMap(weatherFile);
//        System.out.println(weatherMap);
       
        try {
            String line = null;
            FileReader fileReader = new FileReader(sumFile);
            BufferedReader bufferedReader =  new BufferedReader(fileReader);
            
            FileWriter fileWriter = new FileWriter(outputFile);
            BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
            
            while((line = bufferedReader.readLine()) != null) {
                String[] strArray = line.split("\t");
                String date = strArray[1].substring(0, 8);
//                System.out.println(date);
                String weather = weatherMap.get(date);
//                System.out.println(weather);
                
                bufferedWriter.write(line.replaceAll("\t", ", ") + ", " + weather);
                bufferedWriter.newLine();
            }   
            bufferedReader.close();  
            fileReader.close();
            bufferedWriter.close();
            fileWriter.close();
            
        } catch(Exception e) {
            e.printStackTrace();
        }
        
        
    }
    
    public static void generateWeatherMap(String weatherFile) throws Exception {
        
        // This will reference one line at a time
        String line = null;
        try {
            // FileReader reads text files in the default encoding.
            FileReader fileReader = new FileReader(weatherFile);
            // Always wrap FileReader in BufferedReader.
            BufferedReader bufferedReader =  new BufferedReader(fileReader);
            while((line = bufferedReader.readLine()) != null) {
                if(line.length() == 0) {
                    continue;
                }
//                System.out.println(line);
                String[] strArray = line.split(",");
                String date = JoinSumHourAndWeather.formatDate(strArray[0]);
//                System.out.println(date);
                String weather = JoinSumHourAndWeather.formatWeather(strArray[1], strArray[2]);
//                System.out.println(weather);
                weatherMap.put(date, weather);
            }   
            // Always close files.
            bufferedReader.close();  
            fileReader.close();
        }
        catch(FileNotFoundException e) {
            System.out.println("Unable to open file '" + weatherFile + "'");  
            e.printStackTrace();
        }
        catch(IOException e) {
            System.out.println("Error reading file '" + weatherFile + "'"); 
            e.printStackTrace();
        }
        
    }
    
    public static String formatDate(String date) throws ParseException {
        SimpleDateFormat dt1 = new SimpleDateFormat("yyyy/MM/dd");
        SimpleDateFormat dt2 = new SimpleDateFormat("yyyyMMdd");
        Date d = dt1.parse(date);
        return dt2.format(d);
    }
    
    public static String formatWeather(String weather, String temperature) {
        StringBuffer sb = new StringBuffer("");
        String[] w = weather.split("/");
        String[] t = temperature.split("/");
        
        sb.append(w[0]);
        sb.append(", ");
        sb.append(t[0].substring(0, t[0].length()-1));
        sb.append(", ");
        sb.append(w[1]);
        sb.append(", ");
        sb.append(t[1].substring(0, t[1].length()-1));
        
        return sb.toString();
    }
}
